package com.zh.web;

import com.github.pagehelper.PageInfo;
import com.zh.po.Product;
import com.zh.service.ProductService;
import com.zh.web.vo.ResponseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@CrossOrigin
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;
    /**
     * @description: 分页查询所有商品
     * @param: pageNum
     * @param: pageSize
     * @return: com.zh.web.vo.ResponseVO<com.github.pagehelper.PageInfo<com.zh.po.Product>>
     */
    @GetMapping("/page/{pageNum}/{pageSize}")
    public ResponseVO<PageInfo<Product>> page(@PathVariable Integer pageNum,@PathVariable Integer pageSize ){
        PageInfo<Product> products = productService.findAllProduct(pageNum, pageSize);
        return new ResponseVO<>(products,"查询成功",2000);
    }
    /**
     * @description: 查询已上架的所有商品
     * @param: pageNum
     * @param: pageSize
     * @param: productState
     * @return: com.zh.web.vo.ResponseVO<com.github.pagehelper.PageInfo<com.zh.po.Product>>
     */
    @GetMapping(path = {"/pageBuyer/{pageNum}/{pageSize}/{productState}","/pageBuyer/{pageNum}/{pageSize}/{productState}/{productName}"})
    public ResponseVO<PageInfo<Product>> page(@PathVariable Integer pageNum,@PathVariable Integer pageSize,@PathVariable Integer productState,@PathVariable(required = false) String productName){
        PageInfo<Product> products = productService.findProductsByProductName(pageNum, pageSize,productName);
        return new ResponseVO<>(products,"查询成功",2000);
    }

   /**
    * @description: 查询购物车商品
    * @param: pageNum
    * @param: pageSize
    * @param: userId
    * @return: com.zh.web.vo.ResponseVO<com.github.pagehelper.PageInfo<com.zh.po.Product>>
    * @author ふわふわ
    * @date: 2021/5/11 15:16
    */
    @GetMapping("/pageCart/{pageNum}/{pageSize}/{userId}")
    public ResponseVO<PageInfo<Product>> pageCart(HttpServletRequest request, @PathVariable Integer pageNum, @PathVariable Integer pageSize, @PathVariable Integer userId ){
        PageInfo<Product> products = productService.findAllProductCart(pageNum,pageSize,userId);
        ResponseVO<PageInfo<Product>> responseVO = new ResponseVO<>(products, "查询成功", 2000);
        request.getSession().setAttribute("cart",products);
        return responseVO;
    }
    /**
     * @description: 新增商品
     * @param: product
     * @return: com.zh.web.vo.ResponseVO
     */
    @PostMapping
    public ResponseVO addProduct(@RequestBody Product product){
        Integer row = productService.addProduct(product);
        if (row>0){
            return new ResponseVO<>(row,"新增成功",2000);
        }else {
            return new ResponseVO<>(null,"新增失败",4000);
        }
    }
    /**
     * @description: 查询单个商品
     * @param: productId
     * @return: com.zh.web.vo.ResponseVO<com.zh.po.Product>
     */
    @GetMapping("/{productId}")
    public ResponseVO<Product> findProductById(@PathVariable Integer productId){
        Product product = productService.getProductById(productId);
        if (product != null){
            return new ResponseVO<>(product,"查询成功",2000);
        }else {
            return new ResponseVO<>(null,"查询失败",4000);
        }
    }
    /**
     * @description: 批量删除商品
     * @param: ids
     * @return: com.zh.web.vo.ResponseVO
     */
    @DeleteMapping
    public ResponseVO deleteProduct(@RequestBody Integer[] ids){
        for (Integer id : ids) {
            Integer row = productService.deleteProducts(id);
            if (row >0){
                return new ResponseVO<>(row,"删除成功",2000);
            }
        }
        return new ResponseVO<>(null,"删除失败",4000);
    }
    /**
     * @description: 修改商品信息
     * @param: product
     * @return: com.zh.web.vo.ResponseVO
     */
    @PutMapping
    public ResponseVO updateProduct(@RequestBody Product product){
        Integer row = productService.updateProduct(product);
        if (row >0 ){
            return new ResponseVO<>(row,"修改成功",2000);
        }
        return new ResponseVO<>(null,"修改失败",4000);
    }


}
